/*
 * Copyright (C) 2015 UFreedom
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ufreedom.demo.transition;

import com.ufreedom.demo.util.AnimatorStateChangedListener;
import com.ufreedom.floatingview.spring.SimpleReboundListener;
import com.ufreedom.floatingview.spring.SpringHelper;
import com.ufreedom.floatingview.transition.FloatingTransition;
import com.ufreedom.floatingview.transition.YumFloating;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;

/**
 * StarFloating
 *
 * @author UFreedom
 * @since 2021-03-31
 */
public class StarFloating implements FloatingTransition {
    private static final double MBOUNCINESS = 10;
    private static final double MSPEED = 15;
    private static final long MROTATEDURATION = 500L;
    private static final int MROTATEANIMATORVALUE = 360;
    private static final long MTRANSLATEDURATION = 600L;
    private static final long MTRANSLATEANIMATORVALUE = 500L;

    /**
     * applyFloating
     *
     * @param yumFloating
     */
    @Override
    public void applyFloating(final YumFloating yumFloating) {
        SpringHelper.createWithBouncinessAndSpeed(0.0f, 1.0f, MBOUNCINESS, MSPEED)
                .reboundListener(new SimpleReboundListener() {
                    @Override
                    public void onReboundUpdate(double currentValue) {
                        yumFloating.setScaleX((float) currentValue);
                        yumFloating.setScaleY((float) currentValue);
                    }
                }).start(yumFloating);
        AnimatorValue translateAnimator = new AnimatorValue();
        translateAnimator.setDuration(MTRANSLATEDURATION);
        translateAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                yumFloating.setTranslationY(-v * MTRANSLATEANIMATORVALUE);
            }
        });

        translateAnimator.setStateChangedListener(new AnimatorStateChangedListener() {
            @Override
            public void onEnd(Animator animator) {
                yumFloating.setAlpha(0f);
                yumFloating.clear();
            }
        });
        setRotateAnimator(yumFloating).start();
        translateAnimator.start();
    }

    /**
     * setRotateAnimator
     *
     * @param yumFloating
     * @return AnimatorValue
     */
    private AnimatorValue setRotateAnimator(YumFloating yumFloating) {
        AnimatorValue rotateAnimator = new AnimatorValue();

        // 持续时长
        rotateAnimator.setDuration(MROTATEDURATION);

        // 循环次数
        rotateAnimator.setLoopedCount(AnimatorValue.INFINITE);
        rotateAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                yumFloating.setRotation(v * MROTATEANIMATORVALUE);
            }
        });
        return rotateAnimator;
    }
}
